package com.example.administrator.managesystem.presenter.profile;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.example.administrator.managesystem.bean.CodeBean;
import com.example.administrator.managesystem.bean.ProfileInfoBean;
import com.example.administrator.managesystem.bean.PsdSettingBean;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.ui.ProfileView;
import com.example.administrator.managesystem.utils.ToastUtils;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ProfilePresenter {
    private ProfileView profileView;

    public ProfilePresenter(ProfileView profileView) {
        this.profileView = profileView;
    }
    public void psdSettingRequest(String user_id, String token, String psd, String psd_new, String psd_confirm , final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).psdSetting(user_id,token,psd,psd_new,psd_confirm)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<PsdSettingBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(PsdSettingBean psdSettingBean) {
                   if(psdSettingBean.getStatus()==200){
                       ToastUtils.showToast(context,psdSettingBean.getData());
                       profileView.navigateToHome(null);
                   }else{
                       ToastUtils.showToast(context,psdSettingBean.getData());
                   }

                    }

                });

    }

    public void profileInfoRequest(String user_id, String token,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).profileInfoRequest(user_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<ProfileInfoBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(ProfileInfoBean profileInfoBean) {
                       if(profileInfoBean.getStatus()==200){
                           Log.e("msg","profileInfoBean.getData().getUser_label():"+profileInfoBean.getData().getUser_label());
                           profileView.updateUI(profileInfoBean.getData().getHead_img(),profileInfoBean.getData().getAddress(),profileInfoBean.getData().getAge(),
                                   profileInfoBean.getData().getArea_name(),profileInfoBean.getData().getCity_name(),profileInfoBean.getData().getId_card(),
                                   profileInfoBean.getData().getName(),profileInfoBean.getData().getProvince_name(),
                                   profileInfoBean.getData().getRemarks(),profileInfoBean.getData().getSex(),profileInfoBean.getData().getUser_label());
                       }

                    }

                });

    }
}
